package top.yzhao.trackdatasystem.utils;


import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import top.yzhao.trackdatasystem.model.TrackData;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * User: zhaoyang
 * Date: 2020/3/14
 * Time: 13:16
 * Description: No Description
 */
public class FileOpUtils {

    /**
     * 将文件读入内存
     *
     * @param path
     * @return
     */
    public static List<TrackData> readTxt(String path) {
        //String cpath = "classpath:" + path;
        List<TrackData> rs = new ArrayList<>();
        //Resource resource = new ClassPathResource(path);
        //ResourceLoader resourceLoader = new DefaultResourceLoader();
        BufferedReader br = null;
        try {
            //File file = resource.getFile();
            ClassPathResource classPathResource = new ClassPathResource(path);
            br = new BufferedReader(new InputStreamReader(classPathResource.getInputStream(), "UTF-8")); // UTF8
            String s = null;
            while ((s = br.readLine()) != null){
                String[] tmp = s.split(",");
                TrackData td = new TrackData();
                td.setLprf(tmp[1]);
                td.setRprf(tmp[2]);
                td.setLaln(tmp[3]);
                td.setRaln(tmp[4]);
                td.setGage(tmp[5]);
                td.setCant(tmp[6]);
                td.setXlvl(tmp[7]);
                td.setWrap_1(tmp[8]);
                td.setCvtr(tmp[9]);
                td.setLacc_1(tmp[10]);
                td.setVacc(tmp[11]);
                td.setLprfLW_3(tmp[12]);
                td.setRprfLW_3(tmp[13]);
                td.setLalnLW_3(tmp[14]);
                td.setRalnLW_3(tmp[15]);

                rs.add(td);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }


    public static List<String> readgcTxt(String path){
       // String cpath = "classpath:" + path;
        List<String> rs = new ArrayList<>();
        //Resource resource = new ClassPathResource(path);
        //ResourceLoader resourceLoader = new DefaultResourceLoader();
        BufferedReader br = null;
        try {
            //File file = resource.getFile();
            ClassPathResource classPathResource = new ClassPathResource(path);
            br = new BufferedReader(new InputStreamReader(classPathResource.getInputStream(), "UTF-8")); // UTF8
            String s = null;
            while ((s = br.readLine()) != null){
                rs.add(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }


    public static List<String[]> readErrorTxt(String path){
        //String cpath = "classpath:" + path;
        List<String[]> rs = new ArrayList<>();
        //Resource resource = new ClassPathResource(path);
        //ResourceLoader resourceLoader = new DefaultResourceLoader();


        BufferedReader br = null;
        try {
            //File file = resource.getFile();
            //InputStream inputStream = resourceLoader.getResource(cpath).getInputStream();
            ClassPathResource classPathResource = new ClassPathResource(path);
            br = new BufferedReader(new InputStreamReader(classPathResource.getInputStream(), "UTF-8")); // UTF8
            String s = null;
            while ((s = br.readLine()) != null){
                String [] tmp = s.split(" ");
                rs.add(tmp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }

//    public static void main(String[] args) {
//        readErrorTxt("/static/error_txt/1.txt");
//    }
}
